package com.westmarine.qr_hybris.util;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Browser {
	
	public static WebDriver driver = null;
	public enum DriverType {FIREFOX, CHROME, IE, CHROME_MOBILE_ANDROID};
	
	public static WebDriver driver() {
		return driver;
	}
	
	public static void open(String url) {
		driver.get(url);
	}
	
	public static void setDriver(DriverType type) {
		switch(type){
			case FIREFOX: 
				FirefoxProfile newProfile = new FirefoxProfile();
			    newProfile.setPreference("browser.cache.disk.enable", false);
				driver = new FirefoxDriver(newProfile);
				break;
			case CHROME: 
				System.setProperty("webdriver.chrome.driver","lib\\chromedriver.exe");
				driver = new ChromeDriver();
				break;	
			case IE: 
				System.setProperty("webdriver.ie.driver","lib\\IE\\IEDriverServer.exe");
				DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
				caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
				caps.setCapability("ignoreZoomSetting", true);
				driver = new InternetExplorerDriver(caps);
				//driver.get("javascript:document.getElementById('overridelink').click();");
				break;	
			case CHROME_MOBILE_ANDROID:
				DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
				desiredCapabilities.setCapability("device", "Android");
				desiredCapabilities.setCapability("app", "chrome"); 
				URL url = null;
				try {
					url = new URL("http://127.0.0.1:4723/wd/hub");
				} catch (MalformedURLException e) {
					e.printStackTrace();
				} 
				driver = new RemoteWebDriver(url, desiredCapabilities);
				break;
			default: break;
		}
				
	}
	
	public static void close() {
		driver.close();
	}

}
